﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Web.User
{
    public partial class GetPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.SysSetting settings = Zyg.SSO.Core.Business.SysSetting.GetCurrent();
            Copyright.Text = settings.Copyright;
        }

        protected void WriteBackScript(string script)
        {
            if (string.IsNullOrEmpty(script))
                return;

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(),
                String.Format("<script type='text/javascript'>{0}</script>", script));
        }

        /// <summary>
        /// 邮箱
        /// </summary>
        protected void BtnNext1_Click(object sender, EventArgs e)
        {
            if (AccountEmail.Text.Trim() != "")
            {
                Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(AccountEmail.Text);

                if (account == null)
                {
                    WriteBackScript("alert('不存在该用户')");
                    return;
                }

                AccountPassQuestion.Text = account.PassQuestion;

                Step1.Visible = false;
                Step2.Visible = true;
            }
            else
            {
                WriteBackScript("alert('邮箱不能为空');");
            }
        }

        protected void BtnNext2_Click(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(AccountEmail.Text);

            Zyg.Security.SecurityHelper sh = new Zyg.Security.SecurityHelper();

            if (account.PassAnswer == sh.ComputeMD5Hash(AccountPassAnswer.Text.Trim()))
            {
                Step2.Visible = false;
                Step3.Visible = true;
            }
            else
            {
                WriteBackScript("alert('密保答案错误')");
            }
        }

        protected void Btn_Click(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(AccountEmail.Text);
            Zyg.Security.SecurityHelper sh = new Zyg.Security.SecurityHelper();

            if (NewPassword.Text.Trim() == NewPasswordValidate.Text.Trim())
            {
                account.Password = sh.ComputeMD5Hash(NewPassword.Text.Trim());
                WriteBackScript("alert('重设密码成功,现在将跳转至登录页面');window.location.href='../Login.aspx';");
            }
            else
            {
                WriteBackScript("alert('两次密码不一致,请重新输入')");
            }
        }
    }
}
